<?php
class Profile_Controller extends Template_Controller {

	public $template = 'client/index';
	
    public function __construct()
    {
        parent::__construct();
		 //Css
        $this->template->css = $this->site['theme_url'].'login/login.css';
        //View
        $this->get_view();
    }
    
	private function _get_submit()
	{
		if($this->session->get('error_msg'))
			$this->template->content->error_msg = $this->session->get('error_msg');
		if($this->session->get('warning_msg'))
			$this->template->content->warning_msg = $this->session->get('warning_msg');
		if($this->session->get('success_msg'))
			$this->template->content->success_msg = $this->session->get('success_msg');
		if($this->session->get('info_msg'))
			$this->template->content->info_msg = $this->session->get('info_msg');
		if($this->session->get('frm'))			
			$this->template->content->mr = $this->session->get('frm');
	}
	
	private function _list_state()
	{
		$model = new State_Model();
		return $model->get();
	}
	
    public function index()
    {	
        $this->template->content = new View('mypage/frm_profile');
		$this->_get_submit();				
		if($this->login_model->get_login(1))
		{			
			$info = $this->login_model->get_login(1);
			$id = $info['id'];
			$this->template->content->mr = ORM::factory('customer')->find($id)->as_array();			
		} else {
			url::redirect('login');
		}	
		$this->template->content->list_state = $this->_list_state();	
    }
	
    private function _set_form($form)
    {	
    	$record['customers_username'] = $form['txt_username'];
    	$record['customers_email'] = $form['txt_email'];
		$record['customers_firstname'] = $form['txt_firstname'];
		$record['customers_lastname'] = $form['txt_lastname'];
		$record['customers_address'] = $form['txt_address'];
		$record['customers_address2'] = $form['txt_address2'];
		$record['customers_city'] = $form['txt_city'];
		$record['customers_state'] = $form['sel_state'];
		$record['customers_zipcode'] = $form['txt_zipcode'];
		$record['customers_phone'] = $form['txt_phone'];
		$record['customers_fax'] = $form['txt_fax'];
		$record['customers_status'] = 1;
		return $record;
	}
    
	private function _get_record()
    {
    	$form = array
	    (
	    	'txt_username' => '',
	    	'txt_email' => '',
	        'txt_firstname' => '',
	        'txt_lastname' => '',
			'chk_type' => '',
			//'cb_gender' => '',
	        'txt_address' => '',
	        'txt_address2' => '',
	        'txt_city' => '',
	        'sel_state' => '',
	        'txt_zipcode' => '',
	        'txt_phone' => '',
	        'txt_fax' => '',
	    );
	    
	    $errors = $form;
		
		if($_POST)
    	{
    		$post = new Validation($_POST);
    		
			$post->pre_filter('trim', TRUE);
			$post->add_rules('txt_username','required');
			$post->add_rules('txt_email','required','email');
			$post->add_rules('txt_username','required');
			
			$form = arr::overwrite($form, $post->as_array());
			$form = $this->_set_form($form);
			if($post->validate())
 			{
 				return $form; 				
			} else {
				//$this->session->set_flash('frm',$form);
				$errors = arr::overwrite($errors, $post->errors('profile_validation'));
				$str_error = '';
				foreach($errors as $id => $name) if($name) $str_error.='<br>'.$name;
				$this->session->set_flash('error_msg',$str_error);
				
				url::redirect('profile');	
				die();
			}		
        }
    }
    
	public function sm()
    {    	
    	$record = $this->_get_record();
    	
    	if($record)
    	{
    		$cus = $this->login_model->get_login(1);
			$hd_id = $cus['id'];
			
			$query = $this->db->update('customers', $record, array('customers_id' => $hd_id));
			
			$this->_update_sess($hd_id);
			
			$this->session->set_flash('success_msg',Kohana::lang('profile_lang.msg_change_successful'));
			
			url::redirect('profile');
			die();
    	}
    }
    
    private function _update_sess($id)
	{    	
		$cus = ORM::factory('customer')->find($id)->as_array();
        //User
        $this->sess_cus['username'] = $cus['customers_username'];
        $this->sess_cus['name'] = $cus['customers_firstname'];
		$this->sess_cus['email'] = $cus['customers_email'];
        $this->login_model->set_login(1,$this->sess_cus);
		//Info
		$record = array(
			'billing_firstname' => $cus['customers_firstname'],
			'billing_lastname' => $cus['customers_lastname'],
			'billing_email' => $cus['customers_email'],
			'billing_phone' => $cus['customers_phone'],
			
			'billing_address' => $cus['customers_address'],
			'billing_address2' => $cus['customers_address2'],
			'billing_city' => $cus['customers_city'],
			'billing_postcode' => $cus['customers_zipcode'],
			'billing_state' => $cus['customers_state'],
		);	
        $this->session->set('info',$record);
	}
}
?>